Practical XMPP by 2016
Author:2016
Language: eng
Format: epub, mobi
Publisher: Packt Publishing
The middle part isn't really XMPP-related, but we need to handle the room selection and nickname validation before attempting to join the room. So, we need to do a few things: first, when the user clicks on a room, add a selected CSS class to the <li/> element (and clear them from any other items in the list); second, store the selected room name; and third, on clicking the button, we need to check that the user has entered a nickname and selected a room (otherwise, we'll add a popup error message).
First to the <li/> element click:
$(document).on('click', '#room-list li', function(e) { /* Clear any existing selected rooms */ $('#room-list li.selected').attr('class', '') /* Add'selected'CSS class to our new room */ $(e.target).attr('class', 'selected') console.log('Chosen room is now', $(e.target).attr('data-jid')) })
Next, we'll listen for a click on the <button/> and validate that the user has provided the required information:
$('#room-list button').click(function() { var chosenRoom = $('#room-list li.selected') var nickname = $('#room-list input').val() if (0 === chosenRoom.length) { return alert('You must select a room') } if (!nickname) { return alert('You must enter a nickname') } var roomJid = chosenRoom.attr('data-jid') joinChatRoom(roomJid) /* not written yet! */ })
Cool! Now we're ready for the final step of attempting to join the room itself. For this, we use the XMPP-FTW event of xmpp.muc.join. We'll assume that if we start receiving presences (which are sent as xmpp.muc.roster events), then we've joined the room successfully; if not, we'll show the error message to the user. In our example, we'll simulate a nickname conflict.
Picking up from our preceding function, let's make a call to join the room and start things up:
var joinChatRoom = function(roomJid, nickname) { var request = { room: roomJid, nick: nickname } window.mucDetails = request socket.send('xmpp.muc.join', request) } socket.on('xmpp.muc.error', function(e) { console.error('Chat room error', e) if (e.error.condition === 'conflict') { return alert('Nickname already in use') } alert('Chat room error: ' + e.error.condition) }) socket.on('xmpp.muc.roster', function(user) { showChatWindow() /* not written yet */ addUser(user) /* not written yet */ })
Now let's simulate our nickname conflict. Errors in XMPP follow a standard format that is described in RFC-6120, but for those more familiar with HTTP error codes, you may find XEP-0086 (http://xmpp.org/extensions/xep-0086.html) useful to review. This also shows the format of XMPP errors. In the case of a nickname conflict, we'll receive an error with a <condition/> child element with a value of conflict. In our preceding function, we look for this and report back to the user of this issue.
Let's log into a chat room with our test user from Empathy, and then attempt to join the same room using our new web-based UI:
Download
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.
The Mikado Method by Ola Ellnestam Daniel Brolund(21660)
Hello! Python by Anthony Briggs(20890)
Secrets of the JavaScript Ninja by John Resig Bear Bibeault(19357)
Dependency Injection in .NET by Mark Seemann(18941)
The Well-Grounded Java Developer by Benjamin J. Evans Martijn Verburg(18567)
Kotlin in Action by Dmitry Jemerov(18354)
OCA Java SE 8 Programmer I Certification Guide by Mala Gupta(18164)
Algorithms of the Intelligent Web by Haralambos Marmanis;Dmitry Babenko(17026)
Adobe Camera Raw For Digital Photographers Only by Rob Sheppard(16951)
Grails in Action by Glen Smith Peter Ledbrook(16143)
Sass and Compass in Action by Wynn Netherland Nathan Weizenbaum Chris Eppstein Brandon Mathis(13828)
Secrets of the JavaScript Ninja by John Resig & Bear Bibeault(11830)
Test-Driven iOS Development with Swift 4 by Dominik Hauser(10650)
A Developer's Guide to Building Resilient Cloud Applications with Azure by Hamida Rebai Trabelsi(10591)
Jquery UI in Action : Master the concepts Of Jquery UI: A Step By Step Approach by ANMOL GOYAL(9754)
Hit Refresh by Satya Nadella(9100)
The Kubernetes Operator Framework Book by Michael Dame(8534)
Exploring Deepfakes by Bryan Lyon and Matt Tora(8358)
Robo-Advisor with Python by Aki Ranin(8302)